//*************************************************************************************
//* 作    者： zouhunter
//* 创建时间： 2021-11-09 05:00:26
//* 描    述： 按id播放特效

//* ************************************************************************************
using UnityEngine;

public class IDEffectPlayBehaviour : MonoBehaviour
{
    [SerializeField]
    protected int m_effectId;
    [SerializeField]
    protected Vector3 m_localEffectPos;
    [SerializeField]
    protected bool m_playOnEnable;

    public int EffectId => m_effectId;

    private void OnEnable()
    {
        if (m_playOnEnable)
        {
            Play();
        }
    }

    public void Play()
    {
        Play(m_effectId);
    }
    public void Play(int effectId)
    {
        EffectAgent.Instance.Play(effectId, transform.TransformPoint(m_localEffectPos), true);
    }

    [ContextMenu("LoadEffect")]
    public void LoadEffect()
    {
        UFrame.TableCfg.TableController tableCtrl = new UFrame.TableCfg.TableController();
        tableCtrl.LoadTable<UFrame.TableCfg.EffectCfg>(0, "Tables/effect_cfg", (effectTable) =>
        {
            if (effectTable != null)
            {
                var effectCfg = effectTable.GetByID(m_effectId);
                if (effectCfg != null)
                {
                    var pfb = Resources.Load<GameObject>(effectCfg.source);
                    if (pfb)
                    {
                        var instance = Instantiate(pfb);
                        instance.transform.SetParent(transform);
                        instance.transform.position = transform.TransformPoint(m_localEffectPos);
                        return;
                    }
                }
                Debug.LogError("failed load effect!");
            }
            else
            {
                Debug.LogError("failed load effect table!");
            }
        });
    }
}
